﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class analyze_main : CWebBase
{
    CDatabase _db = new CDatabase();
    public CAnalyzeAction action;
    public string _text;
    public string sData;

    public string qs;

    protected void Page_Load(object sender, EventArgs e)
    {
        qs = CheckNull(Request["qs"]);
        _text = CheckNull(Request.Form["text"]);

        if (Request.Files.Get("uploadfile") != null && Request.Files.Get("uploadfile").FileName != "")
        {
            string filename = Request.Files.Get("uploadfile").FileName;
            filename = filename.Substring(filename.LastIndexOf("\\") + 1);

            string fileext = "";

            if (filename.IndexOf(".") > 0)
                fileext = filename.Substring(filename.LastIndexOf(".") + 1).ToLower();

            string filepath = Server.MapPath("/UploadFile/" + Guid.NewGuid().ToString() + "." + fileext);
            Request.Files.Get("uploadfile").SaveAs(filepath);

            if (fileext == "txt")
            {
                _text = ReadTextFile(filepath);
            }
            else if (fileext == "docx")
            {
                DocxToText dtt = new DocxToText(filepath);
                _text = dtt.ExtractText();
            }
            else
                ErrorBack("'" + fileext + "'은(는) 지원하지 않는 확장자입니다.");
        }

        if (_text != "")
        {
            string LevelId = CheckNull(Request.Form["LevelId"]);

            Hashtable selectedData = new Hashtable();

            selectedData.Add("0", CheckNull(Request.Form["sel_Level1"]));
            selectedData.Add("1", CheckNull(Request.Form["sel_Level2"]));
            selectedData.Add("2", LevelId);

            sData = getJson(selectedData);
            action = new CAnalyzeAction(_text, LevelId);
        }
    }

    public bool IsNumber(string pString)
    {
        if (pString == null || pString == "")
            return false;

        string number = "0123456789";

        for (int i = 0; i < pString.Length; i++)
        {
            if (number.IndexOf(pString.Substring(i, 1)) == -1)
            {
                return false;
            }
        }

        return true;
    }

    public String getRange(String min, String max, String post) {
        String result = "-";

        if (!isBlank(min) && !isBlank(max)) {
            result = min + "~" + max + post;
        }

        return result;
    }

    public Boolean isBlank(String str)
    {
        Boolean isnull = true;

        if (str != null)
        {
            if (!str.Trim().Equals(""))
            {
                isnull = false;
            }
        }
        return isnull;
    }

    public String getPercent(double p) { 
        String result = "";
        if (p == null || p <= 0)
        {
            result = "0";
        }
        else {
            result = p.ToString() ;
        }
        return result + "%";
    }

    public String getNumberData(int n) { 
        String result = "0";
        if (n != null)
        {
            result = n.ToString();
        }

        return result;
    }

    public String wordScope(String scope) {
        String baseLevel = "1234567";
        String result = "-";

        if (scope == "8") {
            return "기타";
        }

        if (!isBlank(scope))
        {
            Boolean inEtc = false;

            if (scope.IndexOf("8") >= 0) {
                inEtc = true;
                scope = scope.Replace(",8", "").Replace("8", "");
            }

            string[] nums = scope.Split(',');

            if (nums.Length >= 3)
            {
                string sc = scope.Replace(",", "");
                if (baseLevel.Contains(sc))
                {
                    result = "Level " + nums[0] + "~" + nums[nums.Length - 1];
                }
                else {
                    result = "Level " + scope;
                }
            }
            else {
                result = "Level " + scope;
            }

            if (inEtc)
            {
                result += ",기타";
            }
        }

        
        return result;
    }

    public String wordScopePercent(String scope, Hashtable data) {
        String result = "-";

        if (!isBlank(scope)) {
            String[] sc = scope.Split(',');
            double p = 0.0;

            for (int i = 0; i < sc.Length; i++) {
                if(IsNumber(sc[i])){
                    int s = int.Parse(sc[i]);
                    Distribution d = (Distribution)data[s];
                    p += d.percent;
                }
            }
            result = p + "";
        }
        return result;
    }

    public String isRange(String d, String min, String max, String postStr) {
        if (d == "-") {
            return d;
        }

        String result = "";

        Boolean isIn = false;
        String pre = "<span class=\"red\">";
        String post = "</span>";

        if (isBlank(min) || isBlank(max)) {
            return d;
        }

        double dl = double.Parse(d);
        double dmin = double.Parse(min);
        double dmax = double.Parse(max);

        if (dl <= dmax && dl >= dmin) {
            isIn = true;
        }

        result = d + postStr;

        if (!isIn) {
            result = pre + result + post;
        }

        return result;

    }

    protected void Page_Unload(object sender, EventArgs e)
    {
        if (_db != null)
            _db.ReleaseConnection();
    }
}
